using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BearUtils
{
    public static class UtilTool
    {
        public static Vector2 ToXZ(this Vector3 v3)
        {
            return new Vector2(v3.x, v3.z);
        }

        public static Vector2 Rotate(this Vector3 v3, float degree)
        {
            float nx = v3.x * Mathf.Cos(degree) - v3.y * Mathf.Sin(degree);
            float ny = v3.y * Mathf.Sin(degree) + v3.y * Mathf.Cos(degree);

            return new Vector2(nx, ny);
        }

        public static SerializedVector3 normalized(this SerializedVector3 vec)
        {
            return vec.ToVector3().normalized.FromVector3();
        }

        public static float magnitude(this SerializedVector3 vec)
        {
            return vec.ToVector3().magnitude;
        }

    }

    [Serializable]
    public class SerializedVector3
    {
        public float x;
        public float y;
        public float z;

        public object data;

        public SerializedVector3() { }

        public SerializedVector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public SerializedVector3(Vector3 vector3)
        {
            x = vector3.x;
            y = vector3.y;
            z = vector3.z;
        }

        public static SerializedVector3 operator +(SerializedVector3 v1, SerializedVector3 v2)
        {
            SerializedVector3 tv = new SerializedVector3();
            tv.x = v1.x + v2.x;
            tv.y = v1.y + v2.y;
            tv.z = v1.z + v2.z;

            return tv;
        }

        public static SerializedVector3 operator -(SerializedVector3 v1, SerializedVector3 v2)
        {
            SerializedVector3 tv = new SerializedVector3();
            tv.x = v1.x - v2.x;
            tv.y = v1.y - v2.y;
            tv.z = v1.z - v2.z;

            return tv;
        }

        public static SerializedVector3 operator -(SerializedVector3 v1, Vector3 v2)
        {
            SerializedVector3 tv = new SerializedVector3();
            tv.x = v1.x - v2.x;
            tv.y = v1.y - v2.y;
            tv.z = v1.z - v2.z;

            return tv;
        }

        public static SerializedVector3 operator *(SerializedVector3 v1, float d)
        {
            SerializedVector3 tv = new SerializedVector3();
            tv.x = v1.x * d;
            tv.y = v1.y * d;
            tv.z = v1.z * d;

            return tv;
        }
    }

    public static class Vector3Extensions
    {
        public static Vector3 ToVector3(this SerializedVector3 serializedVector3)
        {
            return new Vector3(serializedVector3.x, serializedVector3.y, serializedVector3.z);
        }

        public static SerializedVector3 FromVector3(this Vector3 vector3)
        {
            return new SerializedVector3(vector3);
        }
    }
}